<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/13
 * Time: 12:09
 */
namespace core;
class Request{
    protected static $pathinfo=null;
    protected static $path=null;
    protected static $instance=null;
    protected static $module=null;
    protected static $controller=null;
    protected static $action=null;

    public static function instance () {
        if(is_null(self::$instance)){
            self::$instance = new static();
        }
        return self::$instance;
    }
    public function pathinfo(){
        if (is_null(self::$pathinfo)) {
            if (isset($_GET[Config::get('config.var_pathinfo')])) {
                // 判断URL里面是否有兼容模式参数
                $_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')];
                unset($_GET[Config::get('var_pathinfo')]);
            } elseif (IS_CLI) {
                // CLI模式下 index.php module/controller/action/params/...
                $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
            }

            self::$pathinfo = empty($_SERVER['PATH_INFO']) ? '/' : ltrim($_SERVER['PATH_INFO'], '/');
        }
        return self::$pathinfo;
    }

    public function path(){
        if (is_null(self::$path)) {
            $suffix   = Config::get('config.url_html_suffix');

            $pathinfo = $this->pathinfo();

            if (false === $suffix) {
                // 禁止伪静态访问
                self::$path = $pathinfo;
            } elseif ($suffix) {
                // 去除正常的URL后缀
                self::$path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
            } else {
                // 允许任何后缀访问
                self::$path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
            }
        }

        return self::$path;
    }

    /**
     * 当前URL的访问后缀
     * @access public
     * @return string
     */
    public function ext()
    {
        return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
    }
}
